package cn.ecpay.unionpay.settle.scheduled;

import cn.ecpay.unionpay.settle.Common.UnionpayDict;
import cn.ecpay.unionpay.settle.dao.SingleFundSettlementDao;
import cn.ecpay.unionpay.settle.entity.db.TPvFundSettleSingle;
import org.apache.commons.lang.StringUtils;
import org.rex.db.exception.DBException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 对账
 * Created by zhaoyanzeng on 2018/12/14.
 */
@Component
public class CheckAccountService {

    @Resource
    private static SingleFundSettlementDao singleFundSettlementDao = new SingleFundSettlementDao();

    /**
     * 对账
     *
     * @param fileName 文件名
     */
    public static void checkAccount(String fileName) {
        List<String> arrayList = readFile(fileName);
        //TODO 第一行是版本，只有一行就跳过，从第二行开始
        if (arrayList.size() == 1)
            return;
        for (int i = 1; i < arrayList.size(); i++) {
            String str = arrayList.get(i);
            System.out.println(str);
            check(str);
        }
    }

    /**
     * 读取文件内容
     *
     * @param fileName
     * @return
     */
    public static ArrayList readFile(String fileName) {
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            File file = new File(fileName);
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), "GBK");
            BufferedReader bf = new BufferedReader(inputReader);
            // 按行读取字符串
            String str;
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
            bf.close();
            inputReader.close();
            return arrayList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 对账
     *
     * @param str
     */
    public static void check(String str) {
        String[] strs = str.split("|");
        String txnType = strs[7];
        String txnNo = strs[8];
        String txnDate = strs[9];
        String txnAmt = strs[4];
        if (UnionpayDict.TXN_TYPE_SINGLEFUNDSETTLEMENT.equals(txnType.trim())) {
            //单笔结算对账
            List<TPvFundSettleSingle> list = null;
            try {
                list = singleFundSettlementDao.findByTxnNo(txnNo, txnDate);
            } catch (DBException e) {
                e.printStackTrace();
            }

            if (list == null) {
                return;
            }
            TPvFundSettleSingle settleSingle = list.get(0);
            //对账失败
            if (StringUtils.isEmpty(txnAmt) || !txnAmt.equals(settleSingle.getTxnAmt())) {
                //交易置失败
            }
        } else if (UnionpayDict.TXN_TYPE_TUIHUI.equals(txnType.trim())) {
            //TODO 退汇

        } else if (UnionpayDict.TXN_TYPE_LAIZHANG.equals(txnType.trim())) {
            //TODO 结算

        }
    }

    public static void main(String[] arg) {
        checkAccount("D:\\IND18120722VACD1100_49984517_0000300000000373");
    }
}
